<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/17 0017
 * Time: 下午 9:24
 */

namespace App\Model;

use EasySwoole\Spl\SplBean;

/**
 * 通用请求参数实体
 * Class ParamsBean
 * @package App\Model
 */
class ParamsBean extends SplBean
{
    protected $fd;
    protected $uniosix; //保存操作码
    protected $controller;
    protected $action;
    protected $platCode;
    protected $userToken;
    protected $routeclientIP;
    protected $user;
    protected $status;
    protected $code;
    protected $msg;
    protected $data;
    protected $gameObject; //实例化对象值
    
    /**
     * @return mixed
     */
    public function getFd()
    {
        return $this->fd;
    }

    /**
     * @param mixed $fd
     */
    public function setFd($fd): void
    {
        $this->fd = $fd;
    }
    /**
     * 获取协议码
     * @return mixed   16进制协议编码
     */
    public function getUniosix()
    {
        return $this->uniosix;
    }
    
    /**
     * 设置协议码
     * @param mixed $uniosix 16进制协议编码
     */
    public function setUniosix($uniosix): void
    {
        $this->uniosix = $uniosix;
    }

    /**
     * @return mixed
     */
    public function getController()
    {
        return $this->controller;
    }

    /**
     * @param mixed $controller
     */
    public function setController($controller): void
    {
        $this->controller = $controller;
    }

    /**
     * @return mixed
     */
    public function getAction()
    {
        return $this->action;
    }

    /**
     * @param mixed $action
     */
    public function setAction($action): void
    {
        $this->action = $action;
    }
    
    
    /**
     * 实例化对象
     */
    public function getGameObject(){
        return $this->gameObject ;
    }
    
    /**
     * 实例化对象
     */
    public function setGameObject($obj = FALSE) :void {
        $this->gameObject = $obj;
    }

    /**
     * @return mixed
     */
    public function getPlatCode()
    {
        return $this->platCode;
    }

    /**
     * @param mixed $platCode
     */
    public function setPlatCode($platCode): void
    {
        $this->platCode = $platCode;
    }

    /**
     * @return mixed
     */
    public function getUserToken()
    {
        return $this->userToken;
    }

    /**
     * @param mixed $userToken
     */
    public function setUserToken($userToken): void
    {
        $this->userToken = $userToken;
    }

    /**
     * @return mixed
     */
    public function getRouteClientIP()
    {
        return $this->routeclientIP;
    }

    /**
     * @param mixed $ClientIP
     */
    public function setRouteClientIP($ip): void
    {
        $this->routeclientIP = $ip;
    }

    /**
     * @return mixed
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param mixed $user
     */
    public function setUser($user): void
    {
        $this->user = $user;
    }

    /**
     * @return mixed
     */
    public function getData($if_array = false)
    {
        if($if_array === true){
            if (is_string($this->data) && !empty($this->data)) {
                return json_decode($this->data,true);
            } 
        }
        return $this->data;
    }

    /**
     * @param mixed $data
     */
    public function setData($data): void
    {
        $this->data = isset($data) && $data ? $data : ['data'=>'init'];
    }

    /**
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param mixed $status
     */
    public function setStatus($status): void
    {
        $this->status = $status;
    }

    /**
     * @return mixed
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @param mixed $code
     */
    public function setCode($code): void
    {
        $this->code = $code;
    }

    /**
     * @return mixed
     */
    public function getMsg()
    {
        return $this->msg;
    }

    /**
     * @param mixed $msg
     */
    public function setMsg($msg): void
    {
        $this->msg = $msg;
    }

    /**
     * 推送过来的数据，全部存储到redis里面
     * @return array
     */
    public function requestJson(){
        $result = [
            'fd' => self::getFd(),
            /*'ctrl' => self::getController(),
            'act' => self::getAction(),*/
            'pc' => self::getPlatCode(),
            'ip' => self::getRouteClientIP(),
            'ut' => self::getUserToken(),
            'user' => self::getUser(),
            'data' => self::getData()
        ];
        return json_encode($result,JSON_UNESCAPED_UNICODE,JSON_UNESCAPED_SLASHES);
    }

    /**
     * 返回的数据
     * @return array
     */
    public function responseJson(){
        $currentCode = self::getCode();
        $result = ['m' => self::getUniosix(),"data"=>""];
        if(!empty($currentCode) && $currentCode !== 200){
            $result["code"] = $currentCode;
            $result["level"] = self::getStatus();  //错误码
            $result["msg"] = self::getMsg();
        }else{
            $result["data"] = self::getData(); 
        }
        return json_encode($result,JSON_UNESCAPED_UNICODE,JSON_UNESCAPED_SLASHES);
    }
}